package com.github.ltsopensource.queue.mysql;

import java.util.Date;

import org.apache.log4j.Logger;

import com.github.ltsopensource.admin.request.BigTaskQueueReq;
import com.github.ltsopensource.core.cluster.Config;
import com.github.ltsopensource.core.support.JobQueueUtils;
import com.github.ltsopensource.queue.BigTaskStore;
import com.github.ltsopensource.queue.mysql.support.RshHolder;
import com.github.ltsopensource.store.jdbc.JdbcAbstractAccess;
import com.github.ltsopensource.store.jdbc.builder.InsertSql;
import com.github.ltsopensource.store.jdbc.builder.SelectSql;

public class BigTaskQueue extends JdbcAbstractAccess implements BigTaskStore{
	private static Logger logger=Logger.getLogger(BigTaskQueue.class);
	public BigTaskQueue(Config config) {
		super(config);
		createTable(readSqlFile("sql/mysql/lts_schedule_task.sql", JobQueueUtils.Big_TASK_STORE));
	}

	@Override
	public int getBigTaskNumById(String bigtaskId) {
		int single = new SelectSql(getSqlTemplate())
		.select()
		.allNum()
		.from()
		.table(getTableName())
		.where("schedule_task_id = ?", bigtaskId)
		.single(RshHolder.SEARCH_NUM);
		return single;
	}

	@Override
	public boolean addBigTask(BigTaskQueueReq request) {
		logger.info("数据库表名："+getTableName());
		System.out.println("数据库表名："+getTableName());
		return new InsertSql(getSqlTemplate())
			.insert(getTableName())
			.columns(
					"schedule_task_id",
					"task_description",
					"task_number",
					"cron",
					"status",
					"priority",
					"task_type",
					"max_time",
					"generater_cmd",
					"crawler_cmd",
					"submit_node_group",
					"execute_node_group",
					"create_time"
					)
			.values(
					request.getTaskId(),
					request.getTaskDes(),
					request.getTaskNum(),
					request.getCronExpression(),
					request.getStatus(),
					request.getTaskPri(),
					request.getTaskType().equals("REAL_TIME_JOB")?0:1,
					request.getTaskMaxTime(),
					request.getTaskSeedCMD(),
					request.getTaskCrawlerCMD(),
					request.getSubmitNodeGroup(),
					request.getTaskTrackerNodeGroup(),
					request.getCreatTime()
					)
			.doInsert()==1;
	}
	
	 private String getTableName() {
	        return JobQueueUtils.Big_TASK_STORE;
	    }

}
